package com.rd.sys.common;

import com.rd.sys.dto.client.store.Material;
import org.apache.struts2.json.annotations.JSON;

import java.io.File;
import java.io.FileFilter;
import java.util.*;

/**
 * @author admin
 * Created time 2019/12/31 9:48
 */
public class FileUtil {

    public static File[] orderByDate(File file) {
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.isDirectory()){
                    return true;
                }
                return false;
            }
        });
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0) {
                    return 1;
                }
                else if (diff == 0) {
                    return 0;
                }
                else {
                    return -1;//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
                }
            }

            @Override
            public boolean equals(Object obj) {
                return true;
            }

        });
        return files;
    }

    public static List<Material> isContains(File[] loadFiles, List<Material> materialFiles){
        List<Material> list = new ArrayList<Material>();

        for (Material material: materialFiles){
            if (!contains(loadFiles, material.getCode())){
                list.add(material);
            }
        }
        return list;
    }

    public static boolean contains(File[] loadFiles, String code){
        for (File file : loadFiles){
            if (file.getName().contains(code)){
                return true;
            }
        }
        return false;
    }


    public static List<Material> reset(List<Material> materials){
        List<Material> reList = new ArrayList<Material>();
        for (Material material : materials){
            if (!reList.contains(material)){
                reList.add(material);
            }
        }
        return reList;
    }
}
